package com.atguigu.gulimall.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * @author kabuda
 * @create 2022-11-26 9:38
 */
public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.......start");
        //CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        //    System.out.println("当前线程号：" + Thread.currentThread().getId());
        //    int i = 10 / 2;
        //    System.out.println("运算结果：" + i);
        //}, executor);

        //CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程号：" + Thread.currentThread().getId());
        //    int i = 10 / 0;
        //    System.out.println("运算结果：" + i);
        //    return i;
        //}, executor).whenComplete((result, exception) -> {
        //    //虽然可以感知异常，但是不能修改返回数据
        //    System.out.println("异步任务完成，结果：" + result + "异常:" + exception);
        //}).exceptionally(throwable -> {
        //    //感知异常，同时返回默认值
        //    return 10;
        //});


        //
        //CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程号：" + Thread.currentThread().getId());
        //    int i = 10 / 0;
        //    System.out.println("运算结果：" + i);
        //    return i;
        //}, executor).handle((result,exception)->{
        //    if(result!=null){
        //        return result*2;
        //    }
        //    if(exception!=null){
        //        return 0;
        //    }
        //    return 666;
        //});


        //CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("当前线程号：" + Thread.currentThread().getId());
        //    int i = 10 / 2;
        //    System.out.println("运算结果：" + i);
        //    return i;
        //}, executor).thenApply(result->{
        //    //如果上个任务有异常就不会运行下一个任务
        //    System.out.println("任务2启动了,上个任务返回值"+result);
        //    return result+6;
        //});
        // Integer result = future.get();
        //System.out.println("main.......end"+result);

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务一结束");
            return i;
        }, executor);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("任务二结束");
            return i;
        }, executor);

        //future1.runAfterBothAsync(future2,()->{
        //    System.out.println("任务三开始");
        //},executor);

        //future1.thenAcceptBothAsync(future2,(f1,f2)->{
        //    System.out.println("任务三开始"+f1+" "+f2);
        //},executor);

        CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
            System.out.println("任务三开始" + f1 + " " + f2);
            return f1 + " " + f2;
        }, executor);

        System.out.println("main.......end"+future.get());


    }

    public static class Thread1 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
        }
    }

    public static class Runnable1 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
        }
    }

    public static class Callable1 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
            return i;
        }
    }
}
